﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 *  
 **************************************** */
using System;
using System.Collections.Generic;
using System.Text;

namespace JYStockAnalyser.Model
{
    /// <summary>
    /// 指标整理器
    /// 将无序、不连续的指标值整理为有序、连续的、可供显示为线性图的指标序列
    /// </summary>
    public class GuideLinesCollator
    {
        /// <summary>
        /// 整理指标
        /// </summary>
        /// <param name="analyseTimeType">分析类型("季度","年")</param>
        /// <param name="beginyear">开始年份</param>
        /// <param name="endYear">结束年份</param>
        /// <param name="type">指标类型</param>
        /// <param name="lines">要整理的指标集合</param>
        public static void CollateLines(string analyseTimeType, YearQuarter beginYearQuarter, YearQuarter endYearQuarter, GuideLineType type,  List<GuideLine> lines)
        {
            if (analyseTimeType.Equals("季度"))
            {
                //首先填充所有时刻的指标，某时刻无指标值，则设为GuideLine.NULL_VALUE
                for (int year = beginYearQuarter.Year; year <= endYearQuarter.Year; year++)
                {
                    for (int quarter = 1; quarter <= 4; quarter++)
                    {
                        if (year == beginYearQuarter.Year && quarter < beginYearQuarter.Quarter) { continue; }

                        if (year == endYearQuarter.Year && quarter == endYearQuarter.Quarter) { break; }

                        GuideLine line = lines.Find(new Predicate<GuideLine>(delegate(GuideLine theline)
                        {
                            return theline.YearQuarter.Year == year && theline.YearQuarter.Quarter == quarter;
                        }));
                        if (line == null)
                        {
                            line = new GuideLine();
                            line.Type = type;
                            line.YearQuarter.Year = year;
                            line.YearQuarter.Quarter = quarter;
                            line.Value = GuideLine.UNDEFINED_VALUE;
                            lines.Add(line);
                        }

                        //if (line.Value.Equals(GuideLine.UNDEFINED_VALUE))
                        //{
                        //    line.Value = 0m;
                        //}
                    }
                }

                //将所有指标排序
                lines.Sort(new Comparison<GuideLine>(delegate(GuideLine line1, GuideLine line2)
                {
                    if (line1.YearQuarter.Year == line2.YearQuarter.Year)
                    {
                        return line1.YearQuarter.Quarter.CompareTo(line2.YearQuarter.Quarter);
                    }
                    else
                    {
                        return line1.YearQuarter.Year.CompareTo(line2.YearQuarter.Year);
                    }
                }));
            }


            //
            //为从开始年份到结束年份中每一年创建“年指标”，并在待整理指标（季度指标）中查找每年的第四季度指标，若存在，则将其设为年指标，若不存在，则将年指标设为空
            //
            if (analyseTimeType.Equals("年"))
            {
                List<GuideLine> newLines = new List<GuideLine>();
                for (int year = beginYearQuarter.Year; year <= endYearQuarter.Year; year++)
                {
                    GuideLine line = new GuideLine();
                    List<GuideLine> searchedlines = lines.FindAll(new Predicate<GuideLine>(delegate(GuideLine theline)
                    {
                        return theline.YearQuarter.Year == year;
                    }));
                    if (searchedlines.Count == 0)
                    {

                        line.Value = GuideLine.UNDEFINED_VALUE;
                    }
                    else
                    {
                        GuideLine yearLine = searchedlines.Find(new Predicate<GuideLine>(delegate(GuideLine theline)
                        {
                            return theline.YearQuarter.Quarter == 4;
                        }));
                        if (yearLine == null)
                        {
                            line.Value = GuideLine.UNDEFINED_VALUE;
                            if (year == endYearQuarter.Year)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            line.Value = yearLine.Value;
                        }
                    }
                    line.Type = type;
                    line.YearQuarter.Year = year;
                    line.YearQuarter.Quarter = YearQuarter.NULL_QUARTER;
                    //if (line.Value.Equals(-77777.77m))
                    //{
                    //    line.Value = 0m;
                    //}
                    newLines.Add(line);
                }

                lines.Clear();
                lines.AddRange(newLines);
            }
        }
    }
}
